#include "CreateTorusWidget.h"

#include "Core/MainWindow.h"
#include "FeatureShape/FeatureShape.h"

CreateTorusWidget::CreateTorusWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateSolidWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateTorusWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreateTorusWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateTorusWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateTorusWidget::onApply);

	connect(ui->radiusOuter, &QDoubleSpinBox::valueChanged, this, &CreateTorusWidget::onSizeChanged);
	connect(ui->radiusInner, &QDoubleSpinBox::valueChanged, this, &CreateTorusWidget::onSizeChanged);

	connect(ui->pX, &QDoubleSpinBox::valueChanged, this, &CreateTorusWidget::onPosChanged);
	connect(ui->pY, &QDoubleSpinBox::valueChanged, this, &CreateTorusWidget::onPosChanged);
	connect(ui->pZ, &QDoubleSpinBox::valueChanged, this, &CreateTorusWidget::onPosChanged);

	connect(ui->selectCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CreateTorusWidget::OnComboBoxIndexChanged);
	m_originPoint = new RWTripleFloatInput(ui->pX, ui->pY, ui->pZ);

	// operation -> param
	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeTorus* TorusShape = static_cast<FeatureShapeTorus*>(CreateOP->m_featureShape.get());
		ui->radiusOuter->setValue(TorusShape->_radiusOuter);
		ui->radiusInner->setValue(TorusShape->_radiusInner);
		m_originPoint->SetValue(TorusShape->_position);
	}

	cacheActor = m_mainWindow->createTorus(ui->radiusOuter->value(), ui->radiusInner->value());
	onPosChanged(0.f);
}

CreateTorusWidget::~CreateTorusWidget()
{
	delete ui;
}

void CreateTorusWidget::onSizeChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	// 这里可能不是value真的发生了变化
	cacheActor = m_mainWindow->UpdateTorusActor(cacheActor, ui->radiusOuter->value(), ui->radiusInner->value());
	onPosChanged(0.f);
}

void CreateTorusWidget::onPosChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	cacheActor = m_mainWindow->UpdateActorPosition(cacheActor, gp_Pnt(ui->pX->value(), ui->pY->value(), ui->pZ->value()));
}

ShapeOperation* CreateTorusWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("圆环"), cacheActor->m_featureShape);
	return op;
}

void CreateTorusWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeTorus(cacheActor->OccShape(), ui->radiusOuter->value(), ui->radiusInner->value()));
}

void CreateTorusWidget::Reset()
{
	QRWCreateSolidWidget::Reset();

	m_originPoint->SetValue(gp_Pnt());
    ui->radiusOuter->setValue(1.f);
    ui->radiusInner->setValue(0.5f);

	cacheActor = m_mainWindow->createTorus(ui->radiusOuter->value(), ui->radiusInner->value());
	onPosChanged(0.f);
}
